package com.heu.blood.bloodtest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.bloodtest.dao.BloodSamplePatientDao;
import com.heu.blood.bloodtest.entity.BloodSamplePatientEntity;
import com.heu.blood.bloodtest.service.BloodSamplePatientService;
import com.heu.blood.bloodtest.vo.BloodSamplePatientReportVo;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("bloodSamplePatientService")
public class BloodSamplePatientServiceImpl extends ServiceImpl<BloodSamplePatientDao, BloodSamplePatientEntity> implements BloodSamplePatientService {

    @Autowired
    private BloodSamplePatientDao bloodSamplePatientDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        BloodSamplePatientEntity bloodSamplePatient = new BloodSamplePatientEntity();
        if(Strings.isNotBlank((String) params.get("bloodSampleId")))
            bloodSamplePatient.setBloodSampleId((String)params.get("bloodSampleId"));
        if(Strings.isNotBlank((String) params.get("patientId")))
            bloodSamplePatient.setPatientId((String)params.get("patientId"));
        if(Strings.isNotBlank((String) params.get("patientName")))
            bloodSamplePatient.setPatientName((String)params.get("patientName"));
        if(Strings.isNotBlank((String) params.get("collector")))
            bloodSamplePatient.setCollector((String)params.get("collector"));
        if(Strings.isNotBlank((String) params.get("recipient")))
            bloodSamplePatient.setRecipient((String)params.get("recipient"));

        //添加时间范围条件
        String collectStartTime = null;
        String collectEndTime = null;
        if(Strings.isNotBlank((String)params.get("collectStartTime")))
            collectStartTime = (String)params.get("collectStartTime");
        if(Strings.isNotBlank((String)params.get("collectEndTime")))
            collectEndTime = (String)params.get("collectEndTime");
        String receivedStartTime = null;
        String receivedEndTime = null;
        if(Strings.isNotBlank((String)params.get("receivedStartTime")))
            receivedStartTime = (String)params.get("receivedStartTime");
        if(Strings.isNotBlank((String)params.get("receivedEndTime")))
            receivedEndTime = (String)params.get("receivedEndTime");


        //构建构造器
        LambdaQueryWrapper<BloodSamplePatientEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodSamplePatient.getBloodSampleId()),BloodSamplePatientEntity::getBloodSampleId,bloodSamplePatient.getBloodSampleId())
                .eq(Strings.isNotBlank(bloodSamplePatient.getPatientId()),BloodSamplePatientEntity::getPatientId,bloodSamplePatient.getPatientId())
                .eq(Strings.isNotBlank(bloodSamplePatient.getPatientName()),BloodSamplePatientEntity::getPatientName,bloodSamplePatient.getPatientName())
                .eq(Strings.isNotBlank(bloodSamplePatient.getCollector()),BloodSamplePatientEntity::getCollector,bloodSamplePatient.getCollector())
                .eq(Strings.isNotBlank(bloodSamplePatient.getRecipient()),BloodSamplePatientEntity::getRecipient,bloodSamplePatient.getRecipient())
                .ge(Strings.isNotBlank(collectStartTime),BloodSamplePatientEntity::getCollectTime,collectStartTime)
                .le(Strings.isNotBlank(collectEndTime),BloodSamplePatientEntity::getCollectTime,collectEndTime)
                .ge(Strings.isNotBlank(receivedStartTime), BloodSamplePatientEntity::getReceivedTime,receivedStartTime)
                .le(Strings.isNotBlank(receivedEndTime), BloodSamplePatientEntity::getReceivedTime,receivedEndTime)
                .eq(BloodSamplePatientEntity::getIfShow, 1);

        IPage<BloodSamplePatientEntity> page = this.page(
                new Query<BloodSamplePatientEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据样本单号查询样本单信息
     * @param bloodSampleId
     * @return
     */
    @Override
    public BloodSamplePatientEntity getBySampleId(String bloodSampleId) {
        // 构建LambdaQueryWrapper对象
        LambdaQueryWrapper<BloodSamplePatientEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(Strings.isNotBlank(bloodSampleId), BloodSamplePatientEntity::getBloodSampleId, bloodSampleId)
                    .eq(BloodSamplePatientEntity::getIfShow, 1);
        // 返回查询结果
        return bloodSamplePatientDao.selectOne(queryWrapper);
    }

    /**
     * 根据样本单号查询检测报告信息
     * @param bloodSampleId
     * @return
     */
    @Override
    public BloodSamplePatientReportVo selectSamplePatientReport(String bloodSampleId) {
        return bloodSamplePatientDao.selectSamplePatientReport(bloodSampleId);
    }

}